package com.lianxi.Courses.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.ResponseCodeConstant;
import com.lianxi.Courses.entity.Chapter;
import com.lianxi.Courses.entity.Course;
import com.lianxi.Courses.entity.CourseType;
import com.lianxi.Courses.exception.ChapterExistException;
import com.lianxi.Courses.exception.CourseExistException;
import com.lianxi.Courses.exception.FileUploadErrorException;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.ChapterService;
import com.lianxi.Courses.service.CourseService;
import com.lianxi.Courses.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Chapter")
public class ChapterController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;


    @RequestMapping("/findAll")
    public ModelAndView findAll(Chapter chapterParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        try {
            PageInfo<Chapter> chapterPage = chapterService.findPage(chapterParam, pageNo, pageSize);
            mav.addObject("chapterPage", chapterPage);
            mav.addObject("chapterParam", chapterParam);
            mav.addObject("courseId", chapterParam.getCourseId());

            mav.setViewName("backend/back_courseReourceSet");
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("errorMessage", "查询章节列表失败，请稍后重试");
            mav.setViewName("backend/back_courseReourceSet");
        }
        return mav;
    }






    @RequestMapping("/addChapter")
    @ResponseBody
    public ResponseResult addChapter(String title, String info, @RequestParam(value = "file", required = false) CommonsMultipartFile file, String resourceTitle,
                              String costType, String costNum, Date createDate, String fileType,
                              HttpSession session,Integer courseId) {
        ResponseResult result = new ResponseResult();

        try {
            System.out.println("接收到添加章节请求：title=" + title + ", courseId=" + courseId + ", file=" + (file != null ? file.getOriginalFilename() : "null"));

            if (file == null || file.isEmpty()) {
                result.setMessage("请选择要上传的文件");
                result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
                return result;
            }

            chapterService.addChapter(title, info, file, resourceTitle, costType, costNum,
                    createDate, fileType, session, courseId);
            result.setMessage("添加章节成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
            e.printStackTrace();
        } catch (ChapterExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            e.printStackTrace();
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常: " + e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }






    @RequestMapping("/modifyStatus")
    @ResponseBody
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            chapterService.modifyStatus(id, status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }




    @RequestMapping("/findById")
    @ResponseBody
    public ResponseResult findById(String id){
        ResponseResult result = new ResponseResult();

        try {
            Chapter chapter = chapterService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(chapter);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }











    @RequestMapping("/modifyChapter")
    @ResponseBody
    public ResponseResult modifyChapter(String id,String title, String info, @RequestParam("file") CommonsMultipartFile file, String resourceTitle,
                              String costType, String costNum, Date createDate, String fileType,
                              HttpSession session,Integer courseId){
        ResponseResult result = new ResponseResult();
        try {
            chapterService.modifyChapter(id,title, info, file, resourceTitle, costType, costNum,
                    createDate, fileType, session, courseId);
            result.setMessage("修改章节成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ChapterExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }






    @RequestMapping("/findByCourseId")
    public ModelAndView findByCourseId(Integer courseId, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        try {
    
            Chapter chapterParam = new Chapter();
            chapterParam.setCourseId(courseId);
          
            PageInfo<Chapter> chapterPage = chapterService.findPage(chapterParam, pageNo, pageSize);
            
           
            mav.addObject("chapterPage", chapterPage);
            mav.addObject("chapterParam", chapterParam);
            mav.addObject("courseId", courseId);
           
            mav.setViewName("backend/back_courseReourceSet");
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("errorMessage", "查询章节列表失败，请稍后重试");
            mav.setViewName("backend/back_courseReourceSet");
        }
        return mav;
    }


























}






    